package smartq.app;

import java.sql.ResultSet;

import smartq.DB.BookQ;
import smartq.DB.Connect;
import smartq.DB.Customers;
import smartq.general.DateTime;

import com.app.smart_q.R;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


@SuppressLint("CutPasteId")
public class SmartQ_Main_Activity extends Activity{
	
	Connect con = new Connect(); 
	ResultSet rs;
	String  OID,
     		UsrOpt,
     		IsActive,
     		IsDeleted,
     		CustomerPhonenumber,
     		CustomerFirstName,
     		CustomerLastName,
     		CustomerSex,
     		CustomerClass,
     		CustomerShop,
     		CustomerFood,
     		CustomerTime,
     		CustomerAlet,
     		CustomerTable,
     		CustomerNumber,
     		CustomerStartTime,
     		CustomerEndTime,
     		CustomerQ,
     		CustomerStatus,
     		CustomerIsMessage,
     		CustomerWaittime;
	
	private EditText Status,Qid;
	private Button btShowdetail,btSmartQ,btNFCConfirm;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smartq_main);
		
		Status = (EditText) findViewById(R.id.smartqmain_status);
		Qid = (EditText) findViewById(R.id.editText_TimeNow);
		btShowdetail = (Button) findViewById(R.id.smartqmain_showdetail);
		btSmartQ = (Button) findViewById(R.id.smartq_insertQ);
		btNFCConfirm = (Button) findViewById(R.id.smartq_cancle);
		
		
	
//	Watch for button clicks.
       Button button = (Button)findViewById(R.id.smartqmain_showdetail);
       button.setOnClickListener(buttonShowDetail);
       
       button = (Button)findViewById(R.id.smartq_insertQ);
       button.setOnClickListener(buttonInsertQ);
       
       button = (Button)findViewById(R.id.smartq_cancle);
       button.setOnClickListener(buttonNFCConfirm);
       
	}
	
	public void onResume(){
		super.onResume();
		Bundle bundle = getIntent().getExtras();
		CustomerPhonenumber = (String) bundle.get("CustomerPhonenumber");
		
		DefineCustomer(CustomerPhonenumber);
		if(CustomerFirstName.equals("")){
			Status.setText("Offline");
			btShowdetail.setEnabled(false);
			btSmartQ.setEnabled(false);
		}else{
			Status.setText("Online");
			Status.setTextColor(Color.BLUE);
			btSmartQ.setEnabled(true);
		}
		
		Qid.setText(CustomerPhonenumber);
		rs = null;
		// TODO Auto-generated method stub
		BookQ usr = new BookQ();
		DateTime dt = new DateTime();
		String DateNow =dt.getDateNowymd();
		try {
			rs = usr.selectBookQ(CustomerPhonenumber,"1", DateNow,"","Waiting");
			if (rs.next() && !Status.getText().toString().equals("Offline") ) {
				btSmartQ.setEnabled(false);
				btNFCConfirm.setEnabled(true);
				btShowdetail.setEnabled(true);
				OID = rs.getString("OID");
				Qid.setText(OID);
				Qid.setTextColor(Color.GREEN);
			}else{
				btNFCConfirm.setEnabled(false);
				btShowdetail.setEnabled(false);
				Qid.setText("ยังไม่ได้จองคิว");
				Qid.setTextColor(Color.RED);
			}
		}catch (Exception e) {
			Status.setText("Offline");
		}
		
	}
    private OnClickListener buttonShowDetail = new OnClickListener() {
        public void onClick(View v) {
        	Intent newActivity = new Intent(SmartQ_Main_Activity.this,SmartQ_ShowDetail_Activity.class);
        	newActivity.putExtra("CustomerPhonenumber", CustomerPhonenumber);
        	startActivity(newActivity);
			finish();
        }
    }; 
    
    private OnClickListener buttonInsertQ = new OnClickListener() {
        public void onClick(View v) {
        	Intent newActivity = new Intent(SmartQ_Main_Activity.this,SmartQ_insert_Activity.class);
//			// input = true;
			newActivity.putExtra("CustomerPhonenumber", CustomerPhonenumber);
			newActivity.putExtra("CustomerFirstName", CustomerFirstName);
			newActivity.putExtra("CustomerLastName", CustomerLastName);
			newActivity.putExtra("CustomerSex", CustomerSex);
			newActivity.putExtra("CustomerClass", CustomerClass);
			startActivity(newActivity);
			finish();
        }
    }; 
    
    
    private OnClickListener buttonNFCConfirm = new OnClickListener() {
        public void onClick(View v) {
        	
        	BookQ usr = new BookQ(
        			Qid.getText().toString(),
					"NFC", 
					"1", 
					null, 
					null,
					null, 
					null,
					null, 
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					"999");

			try {
				if (usr.OperationData("UpdateWaittime") != null) {
					Intent newActivity = new Intent(getApplicationContext(),SmartQ_Tap_Main.class);	                          
                    newActivity.putExtra("CustomerPhonenumber", CustomerPhonenumber);
                    startActivity(newActivity);
                    finish();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
        	
        	
        }
    }; 
    
    
    private void DefineCustomer(String CustomerPhone){
    	rs = null;
    	Customers usr = new Customers();
		try {
			rs = usr.selectCustomers("", "",CustomerPhone);
			
			if (rs.next()) {
				OID = rs.getString("OID");
				CustomerPhonenumber = rs.getString("CustomerPhonenumber");
				CustomerFirstName = rs.getString("CustomerFirstName");
				CustomerLastName = rs.getString("CustomerLastName");
				CustomerSex = rs.getString("Customersex");
				CustomerClass = rs.getString("CustomerClass");
				
			}else{
				CustomerPhonenumber = ("");
				CustomerFirstName = ("");
				CustomerLastName = ("");
				CustomerSex = ("");
				CustomerClass = ("");
			}
		}catch (Exception e) {
			
		}
    }
    
    @Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("ออกจากโปรแกรม")
	        .setMessage("ต้องการออกจากโปรแกรม")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                        finish();
	                        System.exit(0);
	                    }
	                })
	                .setPositiveButton(android.R.string.no, null).create().show();
	    

	} 
	
}
